const router = require('koa-router')();
const utils = require('../utils');
const reportUtils = require('../utils/report');

router.prefix('/data')

router.get('/memory', async (ctx, next) => {
    let servenDay = utils.getServeDay();
    await reportUtils.getDataByDays(servenDay, 'memory')
        .then(res => {
            ctx.body = {
                code: 0,
                type: 'memory',
                data: res
            }
        })
        .catch(e => {
            ctx.body = {
                code: 1,
                message: 'failed'
            }
        })
})
router.get('/static', async (ctx, next) => {
    let servenDay = utils.getServeDay();
    await reportUtils.getDataByDays(servenDay, 'static')
        .then(res => {
            ctx.body = {
                code: 0,
                type: 'static',
                data: res
            }
        })
        .catch(e => {
            ctx.body = {
                code: 1,
                message: 'failed'
            }
        })
})
router.get('/error', async (ctx, next) => {
    let servenDay = utils.getServeDay();
    await reportUtils.getDataByDays(servenDay, 'error')
        .then(res => {
            ctx.body = {
                code: 0,
                type: 'error',
                data: res
            }
        })
        .catch(e => {
            ctx.body = {
                code: 1,
                message: 'failed'
            }
        })
})
router.get('/xhr', async (ctx, next) => {
    let servenDay = utils.getServeDay();
    await reportUtils.getDataByDays(servenDay, 'xhr')
        .then(res => {
            ctx.body = {
                code: 0,
                type: 'xhr',
                data: res
            }
        })
        .catch(e => {
            ctx.body = {
                code: 1,
                message: 'failed'
            }
        })
})
module.exports = router
